import 'dart:io';
import 'dart:typed_data';

import 'package:flutter/material.dart';

import 'package:get/get.dart';
/// 工具
class Tools {
  /// 取消键盘
  static void unfocus() {
    FocusManager.instance.primaryFocus?.unfocus();
  }

  /// double限制
  static String doubleRestrict(double doubleValue) {
    String doubleString = doubleValue.toStringAsFixed(2); // 转换为带有两位小数的字符串
    if (doubleString.endsWith('.00')) {
      // 如果字符串以`.00`结尾，那么它是一个整数，我们去掉小数点和后面的零
      return doubleString.substring(0, doubleString.length - 3);
    } else {
      // 否则，我们保留最多两位小数
      return doubleString;
    }
  }

  String formatInvoiceNumber(int number) {
    if (number < 1000) {
      return "#${number.toString().padLeft(3, '0')}"; // 补0，保证至少3位
    } else {
      return "#$number"; // 1000 及以上直接显示
    }
  }

  // /// 游客关键操作，判断是否登录
  // static Future<bool> touristsKeyOperation() async {
  //   final token = await UserData.instance.getToken();
  //   if (token == null || token.isEmpty) {
  //     final controller = Get.find<MainTabbarLogic>();
  //     final controllerInvoice = Get.find<InvoicesMainLogic>();
  //     final controllerClient = Get.find<ClientsMainLogic>();
  //     final controllerTools = Get.find<ToolsMainLogic>();
  //     final controllerStatistics = Get.find<StatisticsMainLogic>();
  //     final controllerEstimate = Get.find<EstimatesMainLogic>();
  //     controller.sendMessage("你好, GetX!");
  //     // controllerInvoice.sendMessage("你好, GetX!");
  //     //controllerClient.sendMessage("你好, GetX!");
  //     controllerTools.sendMessage("你好, GetX!");
  //     controllerStatistics.sendMessage("你好, GetX!");
  //     // controllerEstimate.sendMessage("你好, GetX!");
  //     await Future.delayed(Duration(milliseconds: 1), () {
  //       Get.offAllNamed(Routes.login)?.then((onValue) {});
  //     });
  //     return false;
  //   }
  //   return true;
  // }

  // compress Uint8List and get another Uint8List.
  // static Future<Uint8List> testComporessList(Uint8List list) async {
  //   var result = await FlutterImageCompress.compressWithList(
  //     list,
  //     minHeight: 1920,
  //     minWidth: 1080,
  //     quality: 80,
  //     rotate: 0,
  //   );
  //   return result;
  // }

  static Future<bool> checkFilePath(String path) async {
    if (path.isEmpty) {
      print("Error: File path is empty!");
      return false;
    }
    final file = File(path);
    if (await file.exists()) {
      return true;
      print("File exists, proceeding with upload...");
    } else {
      return false;
      print("Error: File not found at path: $path");
    }
  }

  // static Future<void> sharePdfFile(String pdfPath) async {
  //   final file = XFile(pdfPath);
  //   await Share.shareXFiles([file]);
  // }
  //
  // static Future<void> requestReview() async {
  //   final InAppReview inAppReview = InAppReview.instance;
  //
  //   if (await inAppReview.isAvailable()) {
  //     // ✅ 在应用内弹出评分窗口（iOS 10.3+ / Android）
  //     await inAppReview.requestReview();
  //   } else {
  //     // ✅ 跳转到 App Store 或 Google Play 评分页面
  //     await inAppReview.openStoreListing(
  //       appStoreId: '6743383890', // 替换成你的 iOS 应用 ID
  //       // microsoftStoreId: 'YOUR_MICROSOFT_STORE_ID', // 可选（仅 Windows）
  //     );
  //   }
  // }
  //
  // static Future<void> openAppStoreRating() async {
  //   final InAppReview inAppReview = InAppReview.instance;
  //   await inAppReview.openStoreListing(appStoreId: '6743383890');
  // }
}
